<?php
/**
 * Created by PhpStorm.
 * User: Yuri
 * DateTime: 2017-4-23 13:49
 */

namespace common\collection;
use Yii;
use common\helpers\PublicFun;
use backend\config\routeParams;
class Common {
    //参数过滤作用
    /**
     * @param $arr
     * @return mixed
     * 对于数组进行sql注入式过滤
     */
    public static function ArrayFilter(&$arr){
        foreach($arr as &$val){
            if(is_array($val)){
                Common::ArrayFilter($val);
            }else {
                if (!get_magic_quotes_gpc()) {
                    $val = addslashes($val);
                }
                if (!is_numeric($val)) {
//                    $val = \mysql_real_escape_string($val);
                }
                $val=trim($val);
            }
        }
        return $arr;
    }
    //获取guid  全球唯一标识
    public static function CreateGuid(){
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = chr(45);
        $uuid = chr(123)
            .substr($charid, 0, 8).$hyphen
            .substr($charid, 8, 4).$hyphen
            .substr($charid,12, 4).$hyphen
            .substr($charid,16, 4).$hyphen
            .substr($charid,20,12)
            .chr(125);
        return $uuid;
    }
    //获取模块信息列表
    public static function GetRoleMenuList()
    {
        $JsonData = [];
        $url = routeParams::HttpUrl('GetRoleMenuList');
        $result = PublicFun::PostPackage($JsonData, $url);
        $result = $result = json_decode($result['Body'],true);
        foreach($result['RoleListResults'] as $key => $val){
            if($val['Id'] == null){
                unset($result['RoleListResults'][$key]);
            }
        }
        function formatTree($array, $pid = 0){
            $arr = array();
            $tem = array();
            foreach ($array as $v) {
                if ($v['PId'] == $pid) {
                    $tem = formatTree($array, $v['Id']);
                    //判断是否存在子数组
                    $tem && $v['childs'] = $tem;
                    $arr[] = $v;
                }
            }
            return $arr;
        }
        $array = formatTree($result['RoleListResults']);
        return $array;
    }

    //获取模块信息列表
    public static function GetMytipId()
    {
        $cookies = Yii::$app->request->cookies;
        $cookie = $cookies->get('MytipIdBack')->value;
//        $cookie = 71;
        return $cookie;
    }
    public static function createDir($strPath, $strMode = 755)
    {
        if(is_dir($strPath)) return TRUE;
        $arrFilePath = explode("/" , $strPath);
        $strFilePath = "";
        for($i = 0 ; $i < count($arrFilePath) ; $i++){
            $strFilePath .= $arrFilePath[$i]."/";
            if(is_dir($strFilePath)){
                continue;
            }
            else{
                mkdir($strFilePath , $strMode);
            }
        }
        return true;
    }


}